package com.pospi.pai.yunpos.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

import com.google.gson.Gson;
import com.lany.sp.SPHelper;
import com.pospi.pai.yunpos.been.PermitonBeen;
import com.pospi.pai.yunpos.login.Constant;

/**
 * Created by acer on 2018/8/31.
 * 收银权限
 */

public class PermitionUtil {

    public PermitonBeen getPermiton() {
        String per = SPHelper.getInstance().getString(Constant.PERMITION);
        PermitonBeen been = new Gson().fromJson(per, PermitonBeen.class);
        return been;
    }

    public void setPermitonHigh() {
        //设置所有权限权限
        PermitonBeen per = new PermitonBeen();
        per.setZkl(0);
        per.setCanDelete(true);
        per.setCanGd(true);
        per.setCanKqx(true);
        per.setCanLx(true);
        per.setCanPrint(true);
        per.setCanSz(true);
        per.setCanTh(true);
        per.setCgdd(true);
        per.setCgsh(true);
        per.setCgtc(true);

        per.setSpsy(true);
        per.setDbck(true);
        per.setDbrk(true);
        per.setDbcy(true);
        per.setPsck(true);
        per.setPsrk(true);
        per.setPscy(true);
        per.setXsdd(true);
        per.setXsck(true);
        per.setXsth(true);
        per.setMdbh(true);
        per.setSspd(true);
        per.setPdlr(true);
        per.setBhjy(true);

        per.setXshz(true);
        per.setSkhz(true);
        per.setXsxp(true);
        per.setXsmx(true);
        per.setKccn(true);
        per.setMdjy(true);
        per.setSpph(true);
        per.setXsfl(true);


        per.setKhwl(true);
        per.setYwywl(true);
        per.setCgmx(true);
        per.setDbmx(true);
        per.setPsmx(true);
        per.setPdmx(true);
        per.setJxcmx(true);
        SPHelper.getInstance().putString(Constant.PERMITION, new Gson().toJson(per, PermitonBeen.class));
    }

    //无权限
    public void setPermitonLow() {
        //设置权限
        PermitonBeen per = new PermitonBeen();
        per.setZkl(0);
        per.setCanDelete(false);
        per.setCanGd(false);
        per.setCanKqx(false);
        per.setCanLx(false);
        per.setCanPrint(false);
        per.setCanSz(false);
        per.setCanTh(false);
        SPHelper.getInstance().putString(Constant.PERMITION, new Gson().toJson(per, PermitonBeen.class));
    }

    public void initPermiton(PermitonBeen been) {
        //设置权限
        PermitonBeen per = new PermitonBeen();
        per.setZkl(been.getZkl());
        per.setCanDelete(been.isCanDelete());
        per.setCanGd(been.isCanGd());
        per.setCanKqx(been.isCanKqx());
        per.setCanLx(been.isCanLx());
        per.setCanPrint(been.isCanPrint());
        per.setCanSz(been.isCanSz());
        per.setCanTh(been.isCanTh());

        per.setCgdd(been.isCgdd());
        per.setCgsh(been.isCgsh());
        per.setCgtc(been.isCgtc());

        per.setSpsy(been.isSpsy());
        per.setDbck(been.isDbck());
        per.setDbrk(been.isDbrk());
        per.setDbcy(been.isDbcy());
        per.setPsck(been.isPsck());
        per.setPsrk(been.isPsrk());
        per.setPscy(been.isPscy());
        per.setXsdd(been.isXsdd());
        per.setXsck(been.isXsck());
        per.setXsth(been.isXsth());
        per.setMdbh(been.isMdbh());
        per.setSspd(been.isSspd());
        per.setPdlr(been.isPdlr());
        per.setBhjy(been.isBhjy());

        per.setXshz(been.isXshz());
        per.setSkhz(been.isSkhz());
        per.setXsxp(been.isXsxp());
        per.setXsmx(been.isXsmx());
        per.setKccn(been.isKccn());
        per.setMdjy(been.isMdjy());
        per.setSpph(been.isSpph());
        per.setXsfl(been.isXsfl());


        per.setKhwl(been.isKhwl());
        per.setYwywl(been.isYwywl());
        per.setCgmx(been.isCgmx());
        per.setDbmx(been.isDbmx());
        per.setPsmx(been.isPsmx());
        per.setPdmx(been.isPdmx());
        per.setJxcmx(been.isJxcmx());

        SPHelper.getInstance().putString(Constant.PERMITION, new Gson().toJson(per, PermitonBeen.class));
    }

    //读写权限
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};
    //请求状态码
    private static int REQUEST_PERMISSION_CODE = 1;
    public void rePermition(Context context){
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions((Activity) context, PERMISSIONS_STORAGE, REQUEST_PERMISSION_CODE);
            }
        }
    }

}
